﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using He.Web.UI;
using He.Common;
using System.IO;

namespace He.Web.user
{
    public partial class reg : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            Model.site_info SiteInfo = new BLL.site_info().GetModelByLang("cn");
            Model.webset SiteConfig = new BLL.webset().GetWebset();

            string checkcode = txtCheckcode.Text.ToLower();

            //检查验证码
            if (Session["VerifyCode"] == null)
            {
                lblMsg.Text = "验证码超时！";
                return;
            }
            if (Session["VerifyCode"].ToString().ToLower() != checkcode)
            {
                lblMsg.Text = "验证码错误或已过期，请重新输入！";
                return;
            }

            BLL.member mgr = new BLL.member();
            Model.member model = new Model.member();
            string username = txtUsername.Text.Trim();
            if (mgr.Exists(username))
            {
                lblMsg.Text = "用户名已经存在，请使用其他用户名！";
                return;
            }
            if (mgr.ExistsEmail(txtEmail.Text.Trim()))
            {
                lblMsg.Text = "该邮箱已被使用，请使用其他邮箱！";
                return;
            }

            model.user_name = username;
            model.user_pwd = CryptoHelper.DESEncrypt(txtUserPwd.Text.Trim());
            model.email = txtEmail.Text.Trim();
            model.reg_time = DateTime.Now.ToString();
            model.status = SiteConfig.member_reg_status;   //初始化为锁定，后台管理员审核通过后解锁
            int uid = mgr.Add(model);
            if (model.status == (int)Enums.MemberStatus.Email验证)
            {
                string random_code = StringHelper.GeneratePassword(15);//随机码，用于重置密码时校验
                model.id = uid;
                model.reset_key = random_code;
                model.reset_expires = DateTime.Now.AddDays(3).ToString();//三天后失效
                mgr.Update(model);

                //读取邮件模板
                string body = MemberHelper.ReadTemplate("~/user/email/activate.html");
                //替换模板标签
                body = MemberHelper.ReplaceTemplate(body, model);

                if (EmailHelper.SendEmail("用户激活", SiteConfig.email_id, CryptoHelper.DESDecrypt(SiteConfig.email_pwd), SiteConfig.email_smtpserver, model.user_name, model.email, "会员账号激活", body, true))
                {
                    Response.Redirect("/user/reg_check.aspx?status=" + (int)Enums.MemberStatus.Email验证 + "&user_name=" + Server.UrlEncode(model.user_name));
                }
                else
                {
                    Response.Redirect("/user/reg_check.aspx?status=" + (int)Enums.MemberStatus.Email验证 + "&user_name=" + Server.UrlEncode(model.user_name) + "&result=error");
                }
            }
            if (model.status == (int)Enums.MemberStatus.人工审核)
            {
                string random_code = StringHelper.GeneratePassword(15);//随机码，用于重置密码时校验
                Response.Redirect("/user/reg_check.aspx?status=" + (int)Enums.MemberStatus.人工审核);
            }
            else if (model.status == (int)Enums.MemberStatus.冻结)
            {
                Response.Redirect("/user/reg_check.aspx?status=" + (int)Enums.MemberStatus.冻结);
            }
            else
            {
                Response.Redirect("/user/reg_success.aspx");
            }
        }
    }
}